﻿using Developer.Entity;
using Super.Framework.Common.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Developer.Service
{
    public partial class SysInfo
    {
        #region 登录用户信息
        /// <summary> 登录用户信息
        /// </summary>
        public static LoginAdmin LoginUser
        {
            get
            {
                var ucs = HttpContext.Current.Request.Cookies["__loginUser"];
                if (!string.IsNullOrEmpty(ucs?.Value))
                {

                    var tokenValue = ucs["Token"];

                    if (!string.IsNullOrEmpty(tokenValue))
                    {
                        var ip = Query.IP;
                        ip = "127.0.0.1";
                        var userAgent = Query.ClientInfos.Agent;

                        var t = HttpUtility.UrlDecode(tokenValue);
                        var user = SecurityService.TokenValid(t, ip, userAgent);
                        if (user == null) return null;

                        return new LoginAdmin()
                        {
                            Id = user.Id,
                            Account = user.Account,
                            Token = tokenValue,
                            Entity = user
                        };

                    }
                }
                return null;
            }

            set
            {
                var cookie = new HttpCookie("__loginUser");
                cookie["Token"] = HttpUtility.UrlEncode(value.Token);
                cookie.Expires = DateTime.Now.AddDays(7);
                HttpContext.Current.Response.SetCookie(cookie);
            }
        }

        public static bool Logout
        {
            get
            {
                Web.Cookies.Del("__loginUser");
                return true;
            }
        }

        #endregion
    }
    public class LoginAdmin
    {
        /// <summary>登录用户Id
        /// </summary>
        public int Id { get; set; }

        public string Account { get; set; }
        /// <summary>
        /// 授权的token
        /// </summary>
        public string Token { get; set; }

        /// <summary> 账号
        /// </summary>
        public Adminer Entity { get; set; }
        
        public int Identity { get; set; }
    }
}
